<?php
class ModelCatalogCategory extends Model {
	public function getCategory($category_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1'");
		
		return $query->row;
	}
	public function get_Banner() {
		$query = $this->db->query("SELECT  * FROM " . DB_PREFIX . "banner where start_date <".time()." and end_date >".time()." and active = 1");
		return $query->rows;
	}
	public function get_Support() {
		$query = $this->db->query("SELECT  * FROM " . DB_PREFIX . "support_online");
		return $query->rows;
	}
	
	public function get_Category() {
		$temp = null;
		$temp_con = null;
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id=0 AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c.status = '1'");
		$j=0;
		$i=0;
		foreach ($query->rows as $cate)
		{
			$temp_query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id= ".$cate['category_id']." AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c.status = '1'");
			if($temp_query->rows!=null) {
			foreach ($temp_query->rows as $temp_cate)
			{
				$temp_con[$i] =array(
				'cate_id' =>$temp_cate['category_id'],
				'cate_name' =>$temp_cate['name'],
				'href' => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $cate['category_id']. '_' . $temp_cate['category_id'].'#right-indent'),
				);
				$i++;
			}
			}
			else 
			{
				$temp_con = null;
			}
			$temp[$j] =array(
			'cate_id' =>$cate['category_id'],
			'cate_name' =>$cate['name'],
			'href' => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $cate['category_id'].'#right-indent'),
			'value' =>$temp_con,
						
			);
			$j++;
			$temp_con = null;
			$i=0;
		}
		return $temp;
	}
	
	public function getCategories($parent_id = 0) {

		$category_data = $this->cache->get('category.' . $parent_id . '.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'));

		if (!$category_data && !is_array($category_data)) {
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '1' AND c.sort_order <> '-1' ORDER BY c.sort_order, LCASE(cd.name)");

			$category_data = $query->rows;

			$this->cache->set('category.' . $parent_id . '.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'), $category_data);
		}

		return $category_data;
	}
				
	public function getTotalCategoriesByCategoryId($parent_id = 0) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1' AND c.sort_order <> '-1'");
		
		return $query->row['total'];
	}
	public function getcategorycha($category)
	{
		$query = $this->db->query("SELECT parent_id FROM " . DB_PREFIX . "category where category_id = ".$category);
		return $query->row['parent_id'];
	}
	public function getcategorybyproduct($product_id)
	{
		$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category where product_id = ".$product_id);
		 
		return $query->row['category_id'];
	}
}
?>